package com.ctsi.unZip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.GZIPInputStream;

import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;

public class FileListener implements FileAlterationListener{     
    public String outputPath;
    public static long count=0;
    public static final String FLUME_SUFFIX=".COMPLETED";
    
    
    public FileListener(String outputPath){
    	super();
    	this.outputPath=outputPath;
    }
    public void onStart(FileAlterationObserver observer) {  
//        System.out.print("#");
    }  
    @Override  
    public void onDirectoryCreate(File directory) {  
        System.out.println("onDirectoryCreate:" +  directory.getName());  
    }  
  
    @Override  
    public void onDirectoryChange(File directory) {  
        System.out.println("onDirectoryChange:" + directory.getName());  
    }  
  
    @Override  
    public void onDirectoryDelete(File directory) {  
        System.out.println("onDirectoryDelete:" + directory.getName());  
    }  
  
    @Override  
    public void onFileCreate(File file) {  
    	System.out.println("onFileCreate:" + file.getName());  
    	unGzFile(file);
    }  
  
    @Override  
    public void onFileChange(File file) {   
    	System.out.println("onFileChange : " + file.getName());
    }  
  
    @Override  
    public void onFileDelete(File file) {  
        System.out.println("onFileDelete :" + file.getName());  
    }  
  
    @Override  
    public void onStop(FileAlterationObserver observer) {  
       // System.out.print("*");  
    }  
    
    public void unGzFile(File file){
    	if(file.getName().contains(".gz")){
    		System.out.println("unGzFile : " + file.getName() +"	"+ ++count);
	        try {   
	            FileInputStream fin = new FileInputStream(file);   
	            GZIPInputStream gzin = new GZIPInputStream(fin);      
	            int i = file.getName().lastIndexOf(".gz");
	            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
	            Date date=new Date();
	            String ts=df.format(date).substring(0,11)+"0";
	            String fileName=file.getName().substring(0, i)+"_"+ts;
	            FileOutputStream fout = new FileOutputStream(outputPath + fileName + FLUME_SUFFIX);
	            byte[] buf=new byte[1024];
	            int num;   
	            while ((num = gzin.read(buf,0,buf.length)) != -1)  
	            {
                    fout.write(buf,0,num);
	            }   
	            gzin.close();
	            fout.close();
	            fin.close();
//	            Thread.sleep(30000);
	            File tmpFile=new File(outputPath + fileName + FLUME_SUFFIX);
	            tmpFile.renameTo(new File(outputPath + fileName));
                file.delete();
	        } catch (Exception ex){  
	            System.err.println(ex.toString());  
	        }  
       }else return;
    }
}  
